package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.enums.activity.eNewServerWelfareActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.pb.activity.TurntableProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.activity.TurntableMgr;
import com.yanqu.road.server.manger.activity.newserverwelfare.NewServerWelfareMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import org.checkerframework.checker.units.qual.C;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_TURNTABLE_LOTTERY, desc = "转盘抽奖")
public class TurntableLotteryCmd extends AbstractCommand {

	private int type;

	private List<Property> rewardList = new ArrayList<>();

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TurntableProto.TurntableLotteryReqMsg reqMsg = TurntableProto.TurntableLotteryReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		Property consume;
		int times;
		if(0 == type){
			consume = new Property(GameConfig.GAME_ITEM_TURNTABLE_LOTTERY, BigInteger.ONE);
			times = 1;
		}else if(1 == type){
			consume = new Property(GameConfig.GAME_ITEM_TURNTABLE_LOTTERY, BigInteger.valueOf(GameConfig.TURNTABLE_LOTTERY_MULTI_CONSUME_COUNT));
			times = 10;
		}else if(2 == type){
			if(VipMgr.getVipLv(player.getUserInfo().getVipExp()) < GameConfig.TURNTABLE_LOTTERY_HUNDRED_NEED_VIP_LEVEL){
				ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;		//vip等级不够
				return;
			}
			consume = new Property(GameConfig.GAME_ITEM_TURNTABLE_LOTTERY, BigInteger.valueOf(GameConfig.TURNTABLE_LOTTERY_HUNDRED_CONSUME_COUNT));
			times = 100;
		}else {
			ret = GameErrorCode.E_TURNTABLE_LOTTERY_ARGUMENT_ERROR;
			return;
		}
		if(!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.Activity, eLogMoneyType.TurntableLottery)){
			player.getModule(BagModule.class).syncBagItem(GameConfig.GAME_ITEM_TURNTABLE_LOTTERY);   //道具不足时候同步一下数据给客户端
			//触发现金礼包
			player.notifyListener(eGamePlayerEventType.RechargeLotteryTimes.getValue(), 0);
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}

		rewardList = player.getModule(PlayerModule.class).doTurntableLottery(type,times,consume);

	}

	@Override
	public void sendData(GamePlayer player) {
		TurntableProto.TurntableLotteryRespMsg.Builder respMsg = TurntableProto.TurntableLotteryRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			if(2 == type){
				Property totalReward = new Property();
				for(Property property : rewardList){
					totalReward.addProperty(property);
				}
				respMsg.addReward(PropertyHelper.parsePropertyToString(totalReward));
			}else {
				for (Property property : rewardList) {
					respMsg.addReward(PropertyHelper.parsePropertyToString(property));
				}
			}
			respMsg.setLuckyValue((int) player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.LuckyValue));
		}
		player.sendPacket(Protocol.U_TURNTABLE_LOTTERY, respMsg);
	}

}
